package com.ds.client.screen;

import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.ds.client.actors.Car;
import com.ds.client.support.ServerStub;


/**
 * choose car screen, used to show choose car information
 */
public class ChooseScreen implements Screen{
	
	InitGame game;
	Image chooseScreen;
	Image chooseLeftBtn;
	Image chooseRightBtn;
	Image chooseFinishBtn;
	Image choosedCarImg;
	Image infoImage;
	private Stage chooseStage;
	int choosedCarIndex = 0;
	ServerStub serverStub;
	
	public ChooseScreen(InitGame initGame){
		game = initGame;
	}

	@Override
	public void dispose() {
		chooseStage.clear();
		
	}

	@Override
	public void hide() {
		
	}

	@Override
	public void pause() {
		
	}

	@Override
	public void render(float arg0) {
		Gdx.gl.glClearColor(255, 255, 255, 255);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		drawchoosedCar();
		chooseStage.draw();
		
	}

	@Override
	public void resize(int arg0, int arg1) {
		
	}

	@Override
	public void resume() {
		
	}

	@Override
	public void show() {
		chooseStage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics
				.getHeight(), false);
		initChoose();
		try {
			serverStub = new ServerStub();
		} catch (Exception e) {
			game.setScreen(game.loseConnScreen);
		}
		
	}
	
	public void initChoose() {
		chooseScreen = new Image(new TextureAtlas(
				"res/chooseScreen/carChoose.pack").findRegion("car-choosing 1"));
		chooseLeftBtn = new Image(new TextureAtlas(
				"res/chooseScreen/chooseleft.pack").findRegion("BUTTONS_left"));
		chooseRightBtn = new Image(new TextureAtlas(
				"res/chooseScreen/chooseright.pack")
				.findRegion("BUTTONS_right"));
		chooseFinishBtn = new Image(new TextureAtlas(
				"res/chooseScreen/chooseright.pack")
				.findRegion("BUTTONS_right"));
		chooseStage.addActor(chooseScreen);
		chooseStage.addActor(chooseLeftBtn);
		chooseStage.addActor(chooseRightBtn);
		chooseStage.addActor(chooseFinishBtn);
		setImagePosition(chooseScreen, 0, 0);
		setImagePosition(chooseLeftBtn, 190, 400);
		setImagePosition(chooseRightBtn, 550, 400);
		setImagePosition(chooseFinishBtn, 750, 0);
		chooseLeftBtn.setClickListener(new ClickListener() {
			@Override
			public void click(Actor actor, float arg1, float arg2) {
				if (choosedCarIndex > 0) {
					choosedCarIndex = --choosedCarIndex % 4;
				} else {
					choosedCarIndex = 0;
				}
			}
		});
		chooseRightBtn.setClickListener(new ClickListener() {
			@Override
			public void click(Actor actor, float arg1, float arg2) {
				choosedCarIndex = ++choosedCarIndex % 4;
			}
		});
		chooseFinishBtn.setClickListener(new ClickListener() {
			@Override
			public void click(Actor actor, float arg1, float arg2) {
				try {
					game.myID = serverStub.register(choosedCarIndex);
				} catch (RemoteException e) {
					game.setScreen(game.loseConnScreen);
				}
				//if the car cannot add into the game
				if (game.myID == -1) {
					System.out.println("failed in enter in game.");
					infoImage = new Image(new TextureAtlas(
							"res/chooseScreen/info.pack").findRegion("info"));
					setImagePosition(infoImage, 0, 0);
					chooseStage.addActor(infoImage);
					infoImage.setClickListener(new ClickListener() {
						@Override
						public void click(Actor actor, float arg1, float arg2) {
							chooseStage.removeActor(infoImage);
						}
					});
				}
				else {
					dispose();
					game.setScreen(game.getWaitingScreen());
					System.out.println("2.");
				}
			}
		});
		Gdx.input.setInputProcessor(chooseStage);

	}
	public void setImagePosition(Image image, float x, float y) {
		image.x = x;
		image.y = y;
	}
	
	public void drawchoosedCar() {
		if (choosedCarImg != null) {
			chooseStage.removeActor(choosedCarImg);
		}
		choosedCarImg = new Image(new TextureAtlas(
				Car.CAR_PATH[choosedCarIndex]).findRegion("1"));
		setImagePosition(choosedCarImg, 370, 400);
		chooseStage.addActor(choosedCarImg);

	}

}
